package secondexecise;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class BinaryTree {
    static class TreeNode{
        public char val;
        private TreeNode left;
        private TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }

    }

    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        B.left = D;
        B.right = E;
        E.right = H;
        A.right = C;
        C.left = F;
        C.right = G;
        return A;
    }

    public void preOrder(TreeNode root){
        if (root == null) return ;
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    public void midOrder(TreeNode root){
        if (root == null) return ;
        midOrder(root.left);
        System.out.print(root.val + " ");
        midOrder(root.right);
    }

    public void postOrder(TreeNode root){
        if (root == null) return ;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    public  int sizeCount;
    public void size1(TreeNode root){
        if (root == null) return;
        sizeCount++;
        size1(root.left);
        size1(root.right);
    }

    public int size2(TreeNode root){
        if (root == null) return 0;
        return size2(root.left) + size2(root.right) + 1;
    }

    public int leafCount;
    public void getLeafCount1(TreeNode root){
        if (root == null) return;
        if (root.left == null && root.right == null) leafCount++;
        getLeafCount1(root.left);
        getLeafCount1(root.right);
    }

    public int getLeafCount2(TreeNode root){
        if (root == null) return 0;
        if (root.left == null && root.right == null) return 1;
        return getLeafCount2(root.left) + getLeafCount2(root.right);
    }

    public int getKLeveCount(TreeNode root, int k){
        if (root == null) return 0;
        if (k == 1) return 1;
        return getKLeveCount(root.left,k-1) + getKLeveCount(root.right,k-1);
    }

    public int getHeight(TreeNode root){
        if (root == null) return 0;
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return Math.max(leftHeight,rightHeight) + 1;
    }

    public TreeNode find(TreeNode root, char val){
        if (root == null) return null;
        if (root.val == val) return root;
        TreeNode leftNode = find(root.left,val);
        if (leftNode != null){
            return leftNode;
        }
        TreeNode rightNode = find(root.right,val);
        if (rightNode != null){
            return rightNode;
        }
        return null;
    }

    public void leveOrder(TreeNode root){
        if (root == null) return;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.peek();
            System.out.print(queue.poll().val + " ");
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }

    public List<List<Character>> levelOrder(TreeNode root) {
        List<List<Character>> list = new ArrayList<>();
        if (root == null) return list;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            List<Character> ret = new ArrayList<>();
            int size = queue.size();
            while (size != 0) {
                TreeNode flag = queue.poll();
                ret.add(flag.val);
                size--;
                if (flag.left != null) {
                    queue.offer(flag.left);
                }
                if (flag.right != null) {
                    queue.offer(flag.right);
                }
            }

            list.add(ret);
        }
        return list;
    }

    public boolean isCompleteTree(TreeNode root) {
        if (root == null) return true;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode ret = queue.poll();
            if (ret == null) break;
            queue.offer(ret.left);
            queue.offer(ret.right);

        }
        while (!queue.isEmpty()) {
            if (queue.poll() != null) return false;
        }
        return true;

    }

}
